package com.boe.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boe.admin.entity.Annex;
import com.boe.admin.mapper.AnnexMapper;
import com.boe.admin.model.request.AnnexRequest;
import com.boe.admin.model.vo.AnnexVO;
import com.boe.admin.model.vo.AnnexUpdateVO;
import com.boe.admin.service.IAnnexService;
import com.boe.common.mvc.ListPageResult;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 * @author Yuxh
 * @since 2022-11-03 13:17
 */
@Service
@RequiredArgsConstructor
public class AnnexServiceImpl extends ServiceImpl<AnnexMapper, Annex> implements IAnnexService {

    @Override
    public ListPageResult<AnnexVO> list(AnnexRequest queryBody, Integer pageNo, Integer pageSize, Map<String, String> order) {

        var queryWapper = Wrappers.<AnnexVO>query();
        if (queryBody != null) {
            if (Strings.isNotEmpty(queryBody.getName())) {
                queryWapper = queryWapper.like("name", queryBody.getName());
            }
        }

        if (order != null) {
            for (var orderKey : order.keySet()) {
                var isDesc = "desc".equalsIgnoreCase(order.get(orderKey));
                if (isDesc) {
                    queryWapper = queryWapper.orderByDesc(orderKey);
                } else {
                    queryWapper = queryWapper.orderByAsc(orderKey);
                }
            }
        }


        var page = new Page<AnnexVO>(pageNo != null ? pageNo : 1, pageSize != null ? pageSize : 99);

        var ret = new ListPageResult<AnnexVO>();
        ret.setData(this.baseMapper.list(page, queryWapper));
        ret.setTotal(page.getTotal());
        return ret;
    }

    @Override
    public boolean update(String id, AnnexUpdateVO annexVO) {

        var annex = new Annex();
        BeanUtils.copyProperties(annexVO, annex);

        var where = Wrappers.<Annex>query();
        where.eq("id", id);

        return this.update(annex, where);
    }
}
